﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Subtitles : System.Web.UI.Page
{
    ManageDatabase data;
    List<Subtitle> subtitles;
    Subtitle oneSub;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (data == null)
        {
            data = new ManageDatabase();
        }
        if (!IsPostBack)
        {
            Initialize();
            GetSeries();
            GetLanguages();  
        }
        Page.Form.Attributes.Add("enctype", "multipart/form-data"); 
        if (!Request.IsAuthenticated)
        {
            SubtitleUploadPanel.Visible = false;
        }
        else {
            SubtitleUploadPanel.Visible = true;
        }
    }

    private void Initialize()
    {
        subtitles = data.GetSubtitlesPreview();
        foreach (Subtitle item in subtitles)
        {
            item.subUrl = "~/Sub/" + item.subUrl;
        }
        subtitlesGridView.DataSource = subtitles;
        subtitlesGridView.DataBind();
    }

    private void GetLanguages()
    {
        ListItemCollection allLanguages = data.GetLanguages();
        LanguageDropDownList.Items.Clear();
        LanguageDropDownList.Items.Add(new ListItem("--Kérem válasszon nyelvet--", "0"));
        foreach (ListItem item in allLanguages)
        {
            LanguageDropDownList.Items.Add(item);
        }
        
    }

    private void GetSeries()
    {
        ListItemCollection allSeries = data.GetSeriesSimple();

        SeriesDropDown.Items.Clear();
        SeriesDropDown.Items.Add(new ListItem("--Sorozat--", "0"));
        SeasonDropDown.Items.Add(new ListItem("--Évad--", "0"));
        EpisodeDropDown.Items.Add(new ListItem("--Epizód--", "0"));
        foreach (ListItem item in allSeries)
        {
            SeriesDropDown.Items.Add(item);
        }

        UpLoadDropDownList1.Items.Clear();
        UpLoadDropDownList1.Items.Add(new ListItem("--Kérem válasszon sorozatot--", "0"));
        UpLoadDropDownList2.Items.Add(new ListItem("--Kérem válasszon évadot--", "0"));
        UpLoadDropDownList3.Items.Add(new ListItem("--Kérem válasszon epizódot--", "0"));
        foreach (ListItem item in allSeries)
        {
            UpLoadDropDownList1.Items.Add(item);
        }
    }

    protected void SeasonChooseSeries_SelectedIndexChanged(object sender, EventArgs e)
    {
        int series_id = Convert.ToInt32(UpLoadDropDownList1.SelectedValue);
        if (series_id == 0)
        {
            UpLoadDropDownList2.Items.Clear();
            UpLoadDropDownList2.Items.Add(new ListItem("--Kérem válasszon évadot--", "0"));
        }
        else
        {
            ListItemCollection allSeason = data.GetSeasonsSimple(series_id);
            UpLoadDropDownList2.Items.Clear();
            UpLoadDropDownList2.Items.Add(new ListItem("--Kérem válasszon évadot--", "0"));
            foreach (ListItem item in allSeason)
            {
                UpLoadDropDownList2.Items.Add(item);
            }
        }
    }

    protected void EpisodeChooseSeason_SelectedIndexChanged(object sender, EventArgs e)
    {
        int series_id = Convert.ToInt32(UpLoadDropDownList2.SelectedValue);
        if (series_id == 0)
        {
            UpLoadDropDownList3.Items.Clear();
            UpLoadDropDownList3.Items.Add(new ListItem("--Kérem válasszon epizódot--", "0"));
        }
        else
        {
            ListItemCollection allEpizodes = data.GetEpisodesSimple(series_id);
            UpLoadDropDownList3.Items.Clear();
            UpLoadDropDownList3.Items.Add(new ListItem("--Kérem válasszon epizódot--", "0"));
            foreach (ListItem item in allEpizodes)
            {
                UpLoadDropDownList3.Items.Add(item);
            }
        }
    }

    protected void SeasonChooseSeries_SelectedIndexChanged2(object sender, EventArgs e)
    {
        int series_id = Convert.ToInt32(SeriesDropDown.SelectedValue);
        if (series_id == 0)
        {
            SeasonDropDown.Items.Clear();
            SeasonDropDown.Items.Add(new ListItem("--Évad--", "0"));
        }
        else
        {
            ListItemCollection allSeason = data.GetSeasonsSimple(series_id);
            SeasonDropDown.Items.Clear();
            SeasonDropDown.Items.Add(new ListItem("--Évad--", "0"));
            foreach (ListItem item in allSeason)
            {
                SeasonDropDown.Items.Add(item);
            }
        }
    }

    protected void EpisodeChooseSeason_SelectedIndexChanged2(object sender, EventArgs e)
    {
        int series_id = Convert.ToInt32(SeasonDropDown.SelectedValue);
        if (series_id == 0)
        {
            EpisodeDropDown.Items.Clear();
            EpisodeDropDown.Items.Add(new ListItem("--Epizód--", "0"));
        }
        else
        {
            ListItemCollection allEpizodes = data.GetEpisodesSimple(series_id);
            EpisodeDropDown.Items.Clear();
            EpisodeDropDown.Items.Add(new ListItem("--Epizód--", "0"));
            foreach (ListItem item in allEpizodes)
            {
                EpisodeDropDown.Items.Add(item);
            }
        }
    }

    protected void SubUpload_Click(object sender, EventArgs e)
    {
        if ((FileUpLoad1.HasFile) && (UpLoadDropDownList3.SelectedValue != "0") && (LanguageDropDownList.SelectedValue != "0"))
        {
            try
            {
                oneSub = new Subtitle();
                oneSub.series = UpLoadDropDownList1.SelectedItem.Text;
                oneSub.season = Convert.ToInt32(UpLoadDropDownList2.SelectedItem.Text);
                oneSub.episode = Convert.ToInt32(UpLoadDropDownList3.SelectedItem.Text);
                oneSub.language = LanguageDropDownList.SelectedItem.Text;
                if (FileUpLoad1.PostedFile.ContentLength < 200000)
                {
                    string filename = UpLoadDropDownList1.SelectedItem.Text + "_" + UpLoadDropDownList2.SelectedItem.Value + "_" +
                        UpLoadDropDownList3.SelectedItem.Value + "_" + LanguageDropDownList.SelectedItem.Text + ".sub";
                    FileUpLoad1.SaveAs(Server.MapPath("~/Sub/") + filename);
                    oneSub.subUrl = filename;
                }
                else
                {
                    UploadLabel.Text = "200kB-nál nagyobb fájl nem tölthető fel!";
                }
                data.InsertSubtitle(oneSub, 1);

                UploadLabel.Text = "Sikeres feltöltés!";
            }
            catch (Exception)
            {
                return;
            }
        }
        else
        {
            UploadLabel.Text = "Jelöljön ki egy fájlt feltöltésre és adja meg a sorozat minden adatát!";
        }
    }

    protected void SearchSubtitle_Click(object sender, EventArgs e)
    {
        String seriesDD = "";
        String seasonDD = "";
        String episodeDD = "";

        try
        {
            if ((SearchPrefix.Text == "") && (SeriesDropDown.SelectedValue == "0"))
            {
                StatusLabel.Text = "Töltse ki a kereső mezőt!";
                Initialize();
                return;
            }
            else
            {
                StatusLabel.Text = "";

                if (SeriesDropDown.SelectedItem.Value == "0")
                {
                    seriesDD = "";
                }
                else
                {
                    seriesDD = SeriesDropDown.SelectedItem.Text;
                }
                if (SeasonDropDown.SelectedItem.Value == "0")
                {
                    seasonDD = "0";
                }
                else
                {
                    seasonDD = SeasonDropDown.SelectedItem.Text;
                }
                if (EpisodeDropDown.SelectedItem.Value == "0")
                {
                    episodeDD = "0";
                }
                else
                {
                    episodeDD = EpisodeDropDown.SelectedItem.Text;
                }
                subtitles = data.GetSearchedSubtitles(SearchPrefix.Text, seriesDD, Convert.ToInt32(seasonDD), Convert.ToInt32(episodeDD));
                foreach (Subtitle item in subtitles)
                {
                    item.subUrl = "~/Sub/" + item.subUrl;
                }
                subtitlesGridView.DataSource = subtitles;
                subtitlesGridView.DataBind();
            }
        }
        catch (Exception)
        {
            Alert.Show("Hiba történt a keresés során!");
        }
    }

    protected void subtitlesGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        subtitlesGridView.PageIndex = e.NewPageIndex;
        //subtitlesGridView.DataSource = GetDataSource();
        subtitlesGridView.DataBind();
    }
}